﻿//------------------------------------------------------------
// Game Framework
// Copyright © 2013-2021 Jiang Yin. All rights reserved.
// Homepage: https://gameframework.cn/
// Feedback: mailto:ellan@gameframework.cn
//------------------------------------------------------------

using UnityEngine;
using UnityGameFramework.Runtime;
using GameEntry = Fruits.Base.GameEntry;

namespace Fruits.BuiltinData
{
    /// <summary>
    /// 自定义数据组件。
    /// </summary>
    public class BuiltinDataComponent : GameFrameworkComponent
    {
        /// <summary>
        /// 用于存储游戏的构建信息（JSON 格式）
        /// </summary>
        [SerializeField]
        private TextAsset m_BuildInfoTextAsset = null;

        /// <summary>
        /// 用于存储默认语言的本地化字典（通常是键值对的 JSON）
        /// </summary>
        [SerializeField]
        private TextAsset m_DefaultDictionaryTextAsset = null;

        /// <summary>
        /// UI界面模板，用于在需要更新资源时显示提示界面
        /// 就一个进度条和描述文本
        /// </summary>
        [Header("下载进度信息")]
        [SerializeField]
        private DownloadForm m_downloadFormTemplate = null;

        private BuildInfo m_BuildInfo = null;

        public BuildInfo BuildInfo
        {
            get
            {
                return m_BuildInfo;
            }
        }

        public DownloadForm DownloadFormTemplate
        {
            get
            {
                return m_downloadFormTemplate;
            }
        }

        public void InitBuildInfo()
        {
            if (m_BuildInfoTextAsset == null || string.IsNullOrEmpty(m_BuildInfoTextAsset.text))
            {
                Log.Info("Build info can not be found or empty.");
                return;
            }

            m_BuildInfo = GameFramework.Utility.Json.ToObject<BuildInfo>(m_BuildInfoTextAsset.text);
            if (m_BuildInfo == null)
            {
                Log.Warning("Parse build info failure.");
                return;
            }
        }

        public void InitDefaultDictionary()
        {
            if (m_DefaultDictionaryTextAsset == null || string.IsNullOrEmpty(m_DefaultDictionaryTextAsset.text))
            {
                Log.Info("Default dictionary can not be found or empty.");
                return;
            }

            if (!GameEntry.Localization.ParseData(m_DefaultDictionaryTextAsset.text))
            {
                Log.Warning("Parse default dictionary failure.");
                return;
            }
        }
    }
}
